package io.renren.modules.bonus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.page.PageData;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.modules.bonus.dao.BonusListDao;
import io.renren.modules.bonus.service.BonusTypeService;
import io.renren.modules.bonus.vo.BonusListVO;
import io.renren.modules.bonus.dto.BonusListDTO;
import io.renren.modules.bonus.entity.BonusListEntity;
import io.renren.modules.bonus.service.BonusListService;
import io.renren.modules.bonus.vo.BonusTypeVO;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * 奖金内容管理
 *
 * @author test test
 * @since 1.0.0 2024-09-10
 */
@Service
public class BonusListServiceImpl extends CrudServiceImpl<BonusListDao, BonusListEntity, BonusListVO> implements BonusListService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private BonusTypeService bonusTypeService;

    @Override
    public QueryWrapper<BonusListEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String bonusTypeId = (String)params.get("bonusTypeId");
        String title = (String)params.get("title");

        QueryWrapper<BonusListEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(bonusTypeId), "bonus_type_id", bonusTypeId);
        wrapper.like(StringUtils.isNotBlank(title), "title", title);

        return wrapper;
    }

    @Override
    public void save(BonusListDTO bonusListDTO) {
        BonusListEntity bonusListEntity = ConvertUtils.sourceToTarget(bonusListDTO, BonusListEntity.class);
        this.insert(bonusListEntity);
    }

    @Override
    public void update(BonusListDTO bonusListDTO) {
        BonusListEntity bonusListEntity = ConvertUtils.sourceToTarget(bonusListDTO, BonusListEntity.class);
        this.updateById(bonusListEntity);
    }

    @Override
    public PageData<BonusListVO> bonusListPage(Map<String, Object> params) {
        List<SysUserDTO> userDtoList = sysUserService.getAll();
        List<BonusTypeVO> bonusTypeVoList = bonusTypeService.getAll();
        PageData<BonusListVO> page = this.page(params);
        page.getList().forEach(e -> {
            BonusTypeVO bonusTypeVo = bonusTypeVoList.stream().filter(j -> j.getId().compareTo(e.getBonusTypeId()) == 0).findAny().orElse(null);
            e.setBonusTypeName(bonusTypeVo == null ? "-" : bonusTypeVo.getTypeName());
            SysUserDTO creator = userDtoList.stream().filter(j -> j.getId().compareTo(e.getCreator()) == 0).findAny().orElse(null);
            e.setCreatorName(creator == null ? "-" : creator.getRealName());
            SysUserDTO updater = userDtoList.stream().filter(j -> j.getId().compareTo(e.getUpdater()) == 0).findAny().orElse(null);
            e.setUpdaterName(updater == null ? "-" : updater.getRealName());
        });
        return page;
    }

    @Override
    public List<BonusListVO> getAll() {
        List<BonusTypeVO> bonusTypeVoList = bonusTypeService.getAll();
        List<BonusListEntity> entityList = this.baseDao.selectList(null);
        List<BonusListVO> bonusListVoList = ConvertUtils.sourceToTarget(entityList, BonusListVO.class);
        bonusListVoList.forEach(e -> {
            BonusTypeVO bonusTypeVo = bonusTypeVoList.stream().filter(j -> j.getId().compareTo(e.getBonusTypeId()) == 0).findAny().orElse(null);
            e.setBonusTypeName(bonusTypeVo == null ? "-" : bonusTypeVo.getTypeName());
        });
        return bonusListVoList;
    }

}